package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.UnionCampWarCampData;
import com.yanqu.road.entity.unioncampwar.UnionCampWarHoldData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarHoldDaoImpl extends TempDataObjectDao<UnionCampWarHoldData> {

    public static String getUnionCampWarHoldDataSql = "select * from `t_c_union_camp_war_hold` where `weekId`=?";

    public Map<Integer, Map<Integer, List<UnionCampWarHoldData>>> getUnionCampWarHoldDataMap(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, List<UnionCampWarHoldData>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getUnionCampWarHoldDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionCampWarHoldData data = parse(rs);
                Map<Integer, List<UnionCampWarHoldData>> centerGroupMap = dataMap.get(data.getCenterGroupId());
                if (null == centerGroupMap) {
                    centerGroupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getCenterGroupId(), centerGroupMap);
                }
                List<UnionCampWarHoldData> holdDataList = centerGroupMap.get(data.getGroupId());
                if (holdDataList == null) {
                    holdDataList = new ArrayList<>();
                    centerGroupMap.put(data.getGroupId(), holdDataList);
                }
                holdDataList.add(data);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUnionCampWarHoldDataSql = "update `t_c_union_camp_war_hold` set campId=?,holdId=?,patronsNum=?,leftHp=?,totalHp=?,lastBattleTime=?,damageMap=? where centerGroupId=? and groupId=? and weekId=? and unionId=?";

    @Override
    protected boolean updateData(UnionCampWarHoldData unionCampWarHoldData) {
        unionCampWarHoldData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getHoldId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getLeftHp()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getTotalHp()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarHoldData.getLastBattleTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarHoldData.getDamageMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarHoldData.getUnionId()));
        if (getDb().executeNoneQuery(updateUnionCampWarHoldDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUnionCampWarHoldDataSql = "insert into `t_c_union_camp_war_hold`(centerGroupId,groupId,weekId,campId,holdId,unionId,patronsNum,leftHp,totalHp,lastBattleTime,damageMap) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionCampWarHoldData unionCampWarHoldData) {
        unionCampWarHoldData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getHoldId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarHoldData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getPatronsNum()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getLeftHp()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarHoldData.getTotalHp()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarHoldData.getLastBattleTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarHoldData.getDamageMap())));
        if (getDb().executeNoneQuery(insertUnionCampWarHoldDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UnionCampWarHoldData parseData(ResultSet rs) throws SQLException {
        UnionCampWarHoldData unionCampWarHoldData = new UnionCampWarHoldData();
        unionCampWarHoldData.setCenterGroupId(rs.getInt("centerGroupId"));
        unionCampWarHoldData.setGroupId(rs.getInt("groupId"));
        unionCampWarHoldData.setWeekId(rs.getInt("weekId"));
        unionCampWarHoldData.setCampId(rs.getInt("campId"));
        unionCampWarHoldData.setHoldId(rs.getInt("holdId"));
        unionCampWarHoldData.setUnionId(rs.getString("unionId"));
        unionCampWarHoldData.setPatronsNum(rs.getInt("patronsNum"));
        unionCampWarHoldData.setLeftHp(rs.getInt("leftHp"));
        unionCampWarHoldData.setTotalHp(rs.getInt("totalHp"));
        unionCampWarHoldData.setLastBattleTime(rs.getLong("lastBattleTime"));
        unionCampWarHoldData.setDamageMap(JSON.parseObject(rs.getString("damageMap"), new TypeReference<java.util.Map<java.lang.Long, java.lang.Integer>>() {
        }));

        return unionCampWarHoldData;
    }
}
